# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
from scrapy.exporters import JsonItemExporter
from twisted.enterprise import adbapi
from twisted.enterprise import adbapi


# 小说
class GetquotesPipeline(object):

    def __init__(self, host, database, user, password, port):
        self.host = host
        self.database = database
        self.user = user
        self.password = password
        self.port = port

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            host=crawler.settings.get('MYSQL_HOST'),
            database=crawler.settings.get('MYSQL_DATABASE'),
            user=crawler.settings.get('MYSQL_USER'),
            password=crawler.settings.get('MYSQL_PASSWORD'),
            port=crawler.settings.get('MYSQL_PORT'),
        )

    def open_spider(self, spider):
        self.db = pymysql.connect(self.host, self.user, self.password, self.database, charset='utf8', port=self.port)
        self.cursor = self.db.cursor()

    def close_spider(self, spider):
        self.db.close()

    def process_item(self, item, spider):
        try:
            name = item['name']
            urls = item['url']
            jianjie = item['jianjie']
            i = range(len(name))
            for num in i:
                sql = 'insert into xiaoshuo(name,url,jianjie) values (%s,%s,%s)'
                self.cursor.execute(sql, (name[num], urls, jianjie))
                self.db.commit()
                print("小说:" + str(name[num]) + "---" + str(urls) + "下载成功！")
                # print(str(item['name'][num])+"---插入成功！")
        except Exception as e:
            print(e)
            pass
        return item

    # 任我看小说


class renwokanPipeline(object):

    def __init__(self, host, database, user, password, port):
        self.host = host
        self.database = database
        self.user = user
        self.password = password
        self.port = port

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            host=crawler.settings.get('MYSQL_HOST'),
            database=crawler.settings.get('MYSQL_DATABASE'),
            user=crawler.settings.get('MYSQL_USER'),
            password=crawler.settings.get('MYSQL_PASSWORD'),
            port=crawler.settings.get('MYSQL_PORT'),
        )

    def open_spider(self, spider):
        self.db = pymysql.connect(self.host, self.user, self.password, self.database, charset='utf8',
                                  port=self.port)
        self.cursor = self.db.cursor()

    def close_spider(self, spider):
        self.db.close()

    def process_item(self, item, spider):
        try:
            name = item['name']
            urls = item['url']
            jianjie = item['jianjie']
            i = range(len(name))
            for num in i:
                sql = 'insert into xiaoshuo(name,url,jianjie) values (%s,%s,%s)'
                self.cursor.execute(sql, (name[num], urls[num], jianjie[num]))
                self.db.commit()
                print("小说:" + str(name[num]) + "---" + str(urls[num]) + "下载成功！")
                # print(str(item['name'][num])+"---插入成功！")
        except Exception as e:
            print(e)
            pass
        return item
